﻿using System;
using System.Collections.Generic;

namespace FWF.Xml
{
    /// <summary>
    /// A wrapper component that contains a xml namespace and a specific xml prefix used when writing xml structures.
    /// </summary>
    public class XmlType : IEqualityComparer<XmlType>
    {

        #region Constructors / Finalizer

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlType"/> class.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="nameSpace">The name space.</param>
        public XmlType( string prefix, string nameSpace )
        {
            this.Prefix = prefix;
            this.Namespace = nameSpace;
        }

        #endregion

        #region IEqualityComparer<XmlType> Members

        /// <summary>
        /// Determines whether the specified objects are equal.
        /// </summary>
        /// <param name="x">The first object of type <paramref name="T"/> to compare.</param>
        /// <param name="y">The second object of type <paramref name="T"/> to compare.</param>
        /// <returns>
        /// true if the specified objects are equal; otherwise, false.
        /// </returns>
        public bool Equals( XmlType x, XmlType y )
        {
            if ( ReferenceEquals( x, null ) )
            {
                return false;
            }
            if ( ReferenceEquals( y, null ) )
            {
                return false;
            }
            if ( ReferenceEquals( x.Namespace, null ) )
            {
                return false;
            }

            return string.Equals( x.Namespace, y.Namespace, StringComparison.OrdinalIgnoreCase );
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        /// The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.
        /// </exception>
        public int GetHashCode( XmlType obj )
        {
            if ( object.ReferenceEquals( obj, null ) )
            {
                return -1;
            }
            if ( object.ReferenceEquals( obj.Namespace, null ) )
            {
                return -1;
            }

            return obj.Namespace.GetHashCode();
        }

        #endregion

        /// <summary>
        /// Gets or sets the namespace.
        /// </summary>
        /// <value>The namespace.</value>
        public string Namespace
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the prefix.
        /// </summary>
        /// <value>The prefix.</value>
        public string Prefix
        {
            get;
            set;
        }

    }
}
